﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsUserCompanyInfo 的摘要说明
    /// <summary>
    public class clsUserCompanyInfo
    {
        #region 构造函数

        public clsUserCompanyInfo() { }

        public clsUserCompanyInfo(int prmUserID)
        {
            clsUserCompanyInfo entity = clsUserCompanyInfo.GetByKey(prmUserID);
            this._userid = entity._userid;
            this._membertype = entity._membertype;
            this._username = entity._username;
            this._companyname = entity._companyname;
            this._CompanyShortName = entity._CompanyShortName;
            this._buildyear = entity._buildyear;
            this._contactperson = entity._contactperson;
            this._tel = entity._tel;
            this._deptname = entity._deptname;
            this._userposition = entity._userposition;
            this._fax = entity._fax;
            this._mobile = entity._mobile;
            this._qq = entity._qq;
            this._email = entity._email;
            this._website = entity._website;
            this._companyaddress = entity._companyaddress;
            this._companytype = entity._companytype;
            this._registeredcapital = entity._registeredcapital;
            this._salestype = entity._salestype;
            this._classcode = entity._classcode;
            this._salesitem = entity._salesitem;
            this._companyabout = entity._companyabout;
            this._bizcardvisible = entity._bizcardvisible;
            this._companyprovince = entity._companyprovince;
            this._companycity = entity._companycity;
            this._viewcount = entity._viewcount;
            this._giftproductcount = entity._giftproductcount;
            this._favoritecount = entity._favoritecount;
        }

        #endregion

        public static clsUserCompanyInfo GetByKey(int prmUserID)
        {
            clsUserCompanyInfo entity = new clsUserCompanyInfo();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader("SELECT * FROM [tbl_UserCompanyInfo] WHERE UserID=" + prmUserID.ToString());
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        private static clsUserCompanyInfo DrToClass(System.Data.IDataReader dr)
        {
            clsUserCompanyInfo entity = new clsUserCompanyInfo();

            int iTmp = 0;
            if (!int.TryParse(dr["UserID"].ToString(), out iTmp))
                iTmp = 0;
            entity._userid = iTmp;
            iTmp = 0;
            if (!int.TryParse(dr["MemberType"].ToString(), out iTmp))
                iTmp = 0;
            entity._membertype = iTmp;
            entity._username = dr["UserName"].ToString();
            entity._companyname = dr["CompanyName"].ToString();
            entity._CompanyShortName = dr["CompanyShortName"].ToString();
            entity._buildyear = dr["BuildYear"].ToString();
            entity._contactperson = dr["ContactPerson"].ToString();
            entity._tel = dr["Tel"].ToString();
            entity._deptname = dr["DeptName"].ToString();
            entity._userposition = dr["UserPosition"].ToString();
            entity._fax = dr["Fax"].ToString();
            entity._mobile = dr["Mobile"].ToString();
            entity._qq = dr["QQ"].ToString();
            entity._email = dr["Email"].ToString();
            entity._website = dr["Website"].ToString();
            entity._companyaddress = dr["CompanyAddress"].ToString();
            entity._companytype = dr["CompanyType"].ToString();
            entity._registeredcapital = dr["RegisteredCapital"].ToString();
            entity._salestype = dr["SalesType"].ToString();
            int intTmp = 0;
            if (!int.TryParse(dr["ClassCode"].ToString(), out intTmp))
                intTmp = 0;
            entity._classcode = intTmp;            
            entity._salesitem = dr["SalesItem"].ToString();
            entity._companyabout = dr["CompanyAbout"].ToString();
            entity._companyprovince = Convert.ToInt32(dr["CompanyProvince"]);
            entity._companycity = Convert.ToInt32(dr["CompanyCity"]);
            bool bTmp = false;
            if (!bool.TryParse(dr["BizCardVisible"].ToString(), out bTmp))
                bTmp = false;
            entity._bizcardvisible = bTmp;
            intTmp = 0;
            if (!int.TryParse(dr["ViewCount"].ToString(), out intTmp))
                intTmp = 0;
            entity._viewcount = intTmp;
            intTmp = 0;
            if (!int.TryParse(dr["GiftProductCount"].ToString(), out intTmp))
                intTmp = 0;
            entity._giftproductcount = intTmp;
            intTmp = 0;
            if (!int.TryParse(dr["FavoriteCount"].ToString(), out intTmp))
                intTmp = 0;
            entity._favoritecount = intTmp;

            return entity;
        }

        #region 增删改数据库操作

        public static int Insert(clsUserCompanyInfo prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_UserCompanyInfo](" +
                            " [UserID]," +
                            " [UserName]," +
                            " [MemberType]," +                            
                            " [CompanyName]," +
                            " [CompanyShortName]," +
                            " [BuildYear]," +
                            " [ContactPerson]," +
                            " [Tel]," +
                            " [DeptName]," +
                            " [UserPosition]," +
                            " [Fax]," +
                            " [Mobile]," +
                            " [QQ]," +
                            " [Email]," +
                            " [Website]," +
                            " [CompanyAddress]," +
                            " [CompanyType]," +
                            " [RegisteredCapital]," +
                            " [SalesType]," +
                            " [ClassCode]," +
                            " [SalesItem]," +
                            " [CompanyAbout]," +
                            " [BizCardVisible]," +
                            " [CompanyProvince]," +
                            " [CompanyCity]," +
                            " [ViewCount]," +
                            " [GiftProductCount]," +
                            " [FavoriteCount]" +
            ") values (" +
                    "@UserID," +
                    "@UserName," +
                    "@MemberType," +
                    "@CompanyName," +
                    "@CompanyShortName," +
                    "@BuildYear," +
                    "@ContactPerson," +
                    "@Tel," +
                    "@DeptName," +
                    "@UserPosition," +
                    "@Fax," +
                    "@Mobile," +
                    "@QQ," +
                    "@Email," +
                    "@Website," +
                    "@CompanyAddress," +
                    "@CompanyType," +
                    "@RegisteredCapital," +
                    "@SalesType," +
                    "@ClassCode," +
                    "@SalesItem," +
                    "@CompanyAbout," +
                    "@BizCardVisible," +
                    "@CompanyProvince," +
                    "@CompanyCity," +
                    "@ViewCount," +
                    "@GiftProductCount," +
                    "@FavoriteCount" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@UserName", prmEntity._username);
            SaveDTM.AddNewParam("@MemberType", prmEntity._membertype);
            SaveDTM.AddNewParam("@CompanyName", prmEntity._companyname);
            SaveDTM.AddNewParam("@CompanyShortName", prmEntity._CompanyShortName);
            SaveDTM.AddNewParam("@BuildYear", prmEntity._buildyear);
            SaveDTM.AddNewParam("@ContactPerson", prmEntity._contactperson);
            SaveDTM.AddNewParam("@Tel", prmEntity._tel);
            SaveDTM.AddNewParam("@DeptName", prmEntity._deptname);
            SaveDTM.AddNewParam("@UserPosition", prmEntity._userposition);
            SaveDTM.AddNewParam("@Fax", prmEntity._fax);
            SaveDTM.AddNewParam("@Mobile", prmEntity._mobile);
            SaveDTM.AddNewParam("@QQ", prmEntity._qq);
            SaveDTM.AddNewParam("@Email", prmEntity._email);
            SaveDTM.AddNewParam("@Website", prmEntity._website);
            SaveDTM.AddNewParam("@CompanyAddress", prmEntity._companyaddress);
            SaveDTM.AddNewParam("@CompanyType", prmEntity._companytype);
            SaveDTM.AddNewParam("@RegisteredCapital", prmEntity._registeredcapital);
            SaveDTM.AddNewParam("@SalesType", prmEntity._salestype);
            SaveDTM.AddNewParam("@ClassCode", prmEntity._classcode);
            SaveDTM.AddNewParam("@SalesItem", prmEntity._salesitem);
            SaveDTM.AddNewParam("@CompanyAbout", prmEntity._companyabout);
            SaveDTM.AddNewParam("@BizCardVisible", prmEntity._bizcardvisible);
            SaveDTM.AddNewParam("@CompanyProvince", prmEntity._companyprovince);
            SaveDTM.AddNewParam("@CompanyCity", prmEntity._companycity);
            SaveDTM.AddNewParam("@ViewCount", prmEntity._viewcount);
            SaveDTM.AddNewParam("@GiftProductCount", prmEntity._giftproductcount);
            SaveDTM.AddNewParam("@FavoriteCount", prmEntity._favoritecount);
            SaveDTM.Insert();
            return 1;
        }

        public static bool Delete(int prmUserID)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_UserCompanyInfo] WHERE UserID=" + prmUserID.ToString());
            dbo.CloseConn();
            dbo = null;
            return true;
        }

        public static bool Update(clsUserCompanyInfo prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = "UPDATE [tbl_UserCompanyInfo] SET " +
                "[UserName] = @UserName, " +
                "[MemberType] = @MemberType, " +
                "[CompanyName] = @CompanyName, " +
                "[CompanyShortName] = @CompanyShortName, " +
                "[BuildYear] = @BuildYear, " +
                "[ContactPerson] = @ContactPerson, " +
                "[Tel] = @Tel, " +
                "[DeptName] = @DeptName, " +
                "[UserPosition] = @UserPosition, " +
                "[Fax] = @Fax, " +
                "[Mobile] = @Mobile, " +
                "[QQ] = @QQ, " +
                "[Email] = @Email, " +
                "[Website] = @Website, " +
                "[CompanyAddress] = @CompanyAddress, " +
                "[CompanyType] = @CompanyType, " +
                "[RegisteredCapital] = @RegisteredCapital, " +
                "[SalesType] = @SalesType, " +
                "[ClassCode] = @ClassCode, " +
                "[SalesItem] = @SalesItem, " +
                "[CompanyAbout] = @CompanyAbout, " +
                "[BizCardVisible] = @BizCardVisible, " +
                "[CompanyProvince] = @CompanyProvince, " +
                "[CompanyCity] = @CompanyCity, " +
                "[ViewCount] = @ViewCount, " +
                "[GiftProductCount] = @GiftProductCount, " +
                "[FavoriteCount] = @FavoriteCount " +
            " WHERE UserID=@UserID";
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserName", prmEntity._username);
            SaveDTM.AddNewParam("@MemberType", prmEntity._membertype);
            SaveDTM.AddNewParam("@CompanyName", prmEntity._companyname);
            SaveDTM.AddNewParam("@CompanyShortName", prmEntity._CompanyShortName);
            SaveDTM.AddNewParam("@BuildYear", prmEntity._buildyear);
            SaveDTM.AddNewParam("@ContactPerson", prmEntity._contactperson);
            SaveDTM.AddNewParam("@Tel", prmEntity._tel);
            SaveDTM.AddNewParam("@DeptName", prmEntity._deptname);
            SaveDTM.AddNewParam("@UserPosition", prmEntity._userposition);
            SaveDTM.AddNewParam("@Fax", prmEntity._fax);
            SaveDTM.AddNewParam("@Mobile", prmEntity._mobile);
            SaveDTM.AddNewParam("@QQ", prmEntity._qq);
            SaveDTM.AddNewParam("@Email", prmEntity._email);
            SaveDTM.AddNewParam("@Website", prmEntity._website);
            SaveDTM.AddNewParam("@CompanyAddress", prmEntity._companyaddress);
            SaveDTM.AddNewParam("@CompanyType", prmEntity._companytype);
            SaveDTM.AddNewParam("@RegisteredCapital", prmEntity._registeredcapital);
            SaveDTM.AddNewParam("@SalesType", prmEntity._salestype);
            SaveDTM.AddNewParam("@ClassCode", prmEntity._classcode);
            SaveDTM.AddNewParam("@SalesItem", prmEntity._salesitem);
            SaveDTM.AddNewParam("@CompanyAbout", prmEntity._companyabout);
            SaveDTM.AddNewParam("@BizCardVisible", prmEntity._bizcardvisible);
            SaveDTM.AddNewParam("@CompanyProvince", prmEntity._companyprovince);
            SaveDTM.AddNewParam("@CompanyCity", prmEntity._companycity);
            SaveDTM.AddNewParam("@ViewCount", prmEntity._viewcount);
            SaveDTM.AddNewParam("@GiftProductCount", prmEntity._giftproductcount);
            SaveDTM.AddNewParam("@FavoriteCount", prmEntity._favoritecount);
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.Update();
            return true;
        }

        #endregion

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="clsid"></param>
        /// <param name="ordertype">
        /// 排序方式
        /// 0 - 按点击数排,1 - 按加入时间排
        /// </param>
        /// <param name="areaid"></param>
        /// <returns></returns>
        public static List<clsUserCompanyInfo> GetCollection(int membertype, int clsid, int ordertype, int areaid)
        {
            string sql = "SELECT * FROM [tbl_UserCompanyInfo] WHERE 1=1 ";
            if (membertype > 0)
                sql += " AND MemberType =" + membertype;
            if (clsid > 0)
                sql += " AND ClassCode &" + clsid + "=" + clsid;
            if (areaid > 0)
                sql += " AND CompanyProvince =" + clsid;
            if (ordertype == 0)
                sql += " ORDER BY ViewCount DESC";
            else
                sql += " ORDER BY UserID DESC";

            return GetCollectionBySQL(sql);
        }

        public static List<clsUserCompanyInfo> GetCollection()
        {
            return GetCollection("");
        }
        public static List<clsUserCompanyInfo> GetCollection(string where)
        {
            string sql = string.Format("SELECT * FROM [tbl_UserCompanyInfo] {0} ORDER BY UserID DESC", where);
            return GetCollectionBySQL(sql);
        }

        private static List<clsUserCompanyInfo> GetCollectionBySQL(string sql)
        {
            List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static int GetCountByCode(int classcode)
        {
            int ret = 0;
            string sql = "SELECT count(1) FROM [tbl_UserCompanyInfo] WHERE membertype=2 ";
            if (classcode > 0)
                sql += " AND ClassCode &" + classcode + "=" + classcode;

            List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            if (dr.Read())
            {
                ret = Convert.ToInt32(dr[0]);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;

            return ret;
        }


        /// <summary>
        ///  处理产品增减对应
        ///  用户产品统计数量变化
        /// </summary>
        public static void DealGiftProductCount(int userid, int s)
        {
            string sql = string.Format("UPDATE [tbl_UserCompanyInfo] SET GiftProductCount=GiftProductCount+{0} WHERE (UserID = {1})", s, userid);
            DBase dbo = new DBase();
            dbo.ExecSql(sql);
            dbo.CloseConn();
            dbo = null;
        }

        /// <summary>
        /// 新增了一个产品
        /// </summary>
        public static void AddGiftProduct(int userid)
        {
            DealGiftProductCount(userid, 1);
        }

        /// <summary>
        /// 删除了一个产品
        /// </summary>
        public static void DecGiftProduct(int userid)
        {
            DealGiftProductCount(userid, -1);
        }

        /// <summary>
        ///  处理被收藏统计数量变化
        /// </summary>
        public static void DealFavoriteCount(int userid, int s)
        {
            string sql = string.Format("UPDATE [tbl_UserCompanyInfo] SET FavoriteCount=FavoriteCount+{0} WHERE (UserID = {1})", s, userid);
            DBase dbo = new DBase();
            dbo.ExecSql(sql);
            dbo.CloseConn();
            dbo = null;
        }

        /// <summary>
        /// 被收藏一次
        /// </summary>
        public static void AddFavorite(int userid)
        {
            DealFavoriteCount(userid, 1);
        }

        /// <summary>
        /// 被取消收藏
        /// </summary>
        public static void DecFavorite(int userid)
        {
            DealFavoriteCount(userid, -1);
        }


        /// <summary>
        /// 商家店铺被关注（点击）一次
        /// </summary>
        public void Hits()
        {
            string sql = string.Format("UPDATE [tbl_UserCompanyInfo] SET ViewCount=ViewCount+1 WHERE (UserID = {0})", this._userid);
            DBase dbo = new DBase();
            dbo.ExecSql(sql);
            dbo.CloseConn();
            dbo = null;
        }

        #region 字段属性

        private int _userid = 0;
        /// <summary>
        /// 用户ID对应UserAccount的用户ID
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private int _membertype = 0;
        /// <summary>
        /// 用户类型：1-经销商用户，2-厂商用户
        /// </summary>
        public int MemberType
        {
            get { return _membertype; }
            set { _membertype = value; }
        }

        private string _username = "";
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return _username; }
            set { _username = value; }
        }

        private string _companyname = "";
        /// <summary>
        /// 企业名称
        /// </summary>
        public string CompanyName
        {
            get { return _companyname; }
            set { _companyname = value; }
        }

        private string _CompanyShortName = "";
        /// <summary>
        /// 企业展厅名称（短）
        /// </summary>
        public string CompanyShortName
        {
            get { return _CompanyShortName; }
            set { _CompanyShortName = value; }
        }

        private string _buildyear = "";
        /// <summary>
        /// 成立年份
        /// </summary>
        public string BuildYear
        {
            get { return _buildyear; }
            set { _buildyear = value; }
        }

        private string _contactperson = "";
        /// <summary>
        /// 联系人
        /// </summary>
        public string ContactPerson
        {
            get { return _contactperson; }
            set { _contactperson = value; }
        }

        private string _tel = "";
        /// <summary>
        /// 固定电话
        /// </summary>
        public string Tel
        {
            get { return _tel; }
            set { _tel = value; }
        }

        private string _deptname = "";
        /// <summary>
        /// 部门
        /// </summary>
        public string DeptName
        {
            get { return _deptname; }
            set { _deptname = value; }
        }

        private string _userposition = "";
        /// <summary>
        /// 职位
        /// </summary>
        public string UserPosition
        {
            get { return _userposition; }
            set { _userposition = value; }
        }

        private string _fax = "";
        /// <summary>
        /// 传真
        /// </summary>
        public string Fax
        {
            get { return _fax; }
            set { _fax = value; }
        }

        private string _mobile = "";
        /// <summary>
        /// 手机
        /// </summary>
        public string Mobile
        {
            get { return _mobile; }
            set { _mobile = value; }
        }

        private string _qq = "";
        /// <summary>
        /// QQ
        /// </summary>
        public string QQ
        {
            get { return _qq; }
            set { _qq = value; }
        }

        private string _email = "";
        /// <summary>
        /// Email
        /// </summary>
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        private string _website = "";
        /// <summary>
        /// 公司网址
        /// </summary>
        public string Website
        {
            get { return _website; }
            set { _website = value; }
        }

        private string _companyaddress = "";
        /// <summary>
        /// 联系地址
        /// </summary>
        public string CompanyAddress
        {
            get { return _companyaddress; }
            set { _companyaddress = value; }
        }

        private string _companytype = "";
        /// <summary>
        /// 企业性质
        /// </summary>
        public string CompanyType
        {
            get { return _companytype; }
            set { _companytype = value; }
        }

        private string _registeredcapital = "";
        /// <summary>
        /// 注册资金
        /// </summary>
        public string RegisteredCapital
        {
            get { return _registeredcapital; }
            set { _registeredcapital = value; }
        }

        private string _salestype = "";
        /// <summary>
        /// 经营类别
        /// </summary>
        public string SalesType
        {
            get { return _salestype; }
            set { _salestype = value; }
        }

        private int _classcode = 0;
        /// <summary>
        /// 经营类别代码
        /// </summary>
        public int ClassCode
        {
            get { return _classcode; }
            set { _classcode = value; }
        }

        private string _salesitem = "";
        /// <summary>
        /// 主营产品或服务
        /// </summary>
        public string SalesItem
        {
            get { return _salesitem; }
            set { _salesitem = value; }
        }

        private string _companyabout = "";
        /// <summary>
        /// 企业简介
        /// </summary>
        public string CompanyAbout
        {
            get { return _companyabout; }
            set { _companyabout = value; }
        }

        private bool _bizcardvisible = false;
        /// <summary>
        /// 企业名片是否可见
        /// </summary>
        public bool BizCardVisible
        {
            get { return _bizcardvisible; }
            set { _bizcardvisible = value; }
        }

        private int _companyprovince = 0;
        /// <summary>
        /// 企业所在地区省
        /// </summary>
        public int CompanyProvince
        {
            get { return _companyprovince; }
            set { _companyprovince = value; }
        }

        private int _companycity = 0;
        /// <summary>
        /// 企业所在地区市
        /// </summary>
        public int CompanyCity
        {
            get { return _companycity; }
            set { _companycity = value; }
        }

        private int _viewcount = 0;
        /// <summary>
        /// 被关注量（点击数）
        /// </summary>
        public int ViewCount
        {
            get { return _viewcount; }
            set { _viewcount = value; }
        }

        private int _giftproductcount = 0;
        /// <summary>
        /// 发布产品数量
        /// </summary>
        public int GiftProductCount
        {
            get { return _giftproductcount; }
            set { _giftproductcount = value; }
        }

        private int _favoritecount = 0;
        /// <summary>
        /// 被收藏数
        /// </summary>
        public int FavoriteCount
        {
            get { return _favoritecount; }
            set { _favoritecount = value; }
        }

        public string CompanyLogo
        {
            get
            {
                string ret = "User/images/logo_sample.jpg";
                clsCompanyPhoto p = new clsCompanyPhoto(this.UserID, 0);
                if (p.CompanyPhotoID > 0 && p.PhotoPath != "")
                    ret = p.PhotoPath;
                return ret;
            }
        }

        #endregion
    }
}
